#include <X11/Xlib.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <X11/Xutil.h>
#include <fstream>
#include <unistd.h>
#include <vector>
#include <string.h>
#include <csignal>
#include <thread>
#include <X11/extensions/XTest.h>
#include <memory>
#include "NetTools.h"
#include "IniFileParser.h"
#include "SignalWraper.h"


class RemoteDesktopServer
{
private:
    Display *display;
    int display_server_fd, controller_server_fd;
    XImage *ximage;
    unsigned short display_port, controller_port;
    pthread_t display_thread, event_thread;
    NetTools net_tools;
    // SignalWraper signal_wraper;

    bool initDisplayServer();
    bool initControllerServer();
    void runDisplay();
    static void *startDisplay(void *arg);
    void simulateKeyPress(KeySym key, bool press);
    void simulateMouseClick(int button, int x, int y, bool press);
    void simulateMouseMotion(int x, int y);
    void runFakeController();
    static void *startFakeController(void *arg);
    // 序列化XImage到字节流
    std::vector<char> serializeImage(const XImage *ximage);

public:
    RemoteDesktopServer(unsigned short display_port, unsigned short controller_port);
    ~RemoteDesktopServer();
    void start();
};
